// DetialDlg.cpp : implementation file
//

#include "stdafx.h"
#include "eWorldMultiView.h"
#include "DetailDlg.h"


// CDetailDlg dialog

IMPLEMENT_DYNAMIC(CDetailDlg, CDialog)

CDetailDlg::CDetailDlg( int nWorldWidth, int nWorldHeight, CeObject** map, CWnd* pParent /*=NULL*/)
	: CDialog(CDetailDlg::IDD, pParent)
	, m_nCellAge(0)
	, m_nCellLife(0)
	, m_nAgeToFind(0)
	, m_nLifeToFind(0)
{
	pObjMap = new E_OBJ_DETAIL[nWorldWidth*nWorldHeight];
	m_nWorldHeight = nWorldHeight;
	m_nWorldWidth = nWorldWidth;

	for( int i=0; i<nWorldWidth*nWorldHeight; i++ )
	{
		pObjMap[i].type = map[i]->GetType();
		pObjMap[i].nAge = map[i]->GetAge();
		pObjMap[i].nLife = pObjMap[i].type==EOBJTYPE_CREATURE ? ((CCreature*)map[i])->GetLife() : 0;
	}
}

CDetailDlg::~CDetailDlg()
{
	delete pObjMap;
}

void CDetailDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_GridControl(pDX, IDC_GRID, m_GridCtrl);
	DDX_Text(pDX, IDC_CELLAGE, m_nCellAge);
	DDX_Text(pDX, IDC_CELLLIFE, m_nCellLife);
	DDX_Control(pDX, IDC_AGERESULTLIST, m_listAgeResult);
	DDX_Text(pDX, IDC_CELLAGE2, m_nAgeToFind);
	DDX_Text(pDX, IDC_CELLLIFE2, m_nLifeToFind);
	DDX_Control(pDX, IDC_LIFERESULTLIST, m_listLifeResult);
}


BEGIN_MESSAGE_MAP(CDetailDlg, CDialog)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_AGESEARCH, &CDetailDlg::OnBnClickedAgesearch)
	ON_LBN_DBLCLK(IDC_AGERESULTLIST, &CDetailDlg::OnLbnDblclkAgeresultlist)
	ON_LBN_SELCHANGE(IDC_AGERESULTLIST, &CDetailDlg::OnLbnSelchangeAgeresultlist)
	ON_BN_CLICKED(IDC_LIFESEARCH, &CDetailDlg::OnBnClickedLifesearch)
	ON_LBN_SELCHANGE(IDC_LIFERESULTLIST, &CDetailDlg::OnLbnSelchangeLiferesultlist)
END_MESSAGE_MAP()


// CDetailDlg message handlers

BOOL CDetailDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

    {
        int m_nFixCols = 1;
	    int m_nFixRows = 1;
	    int m_nCols = m_nWorldWidth+1;
	    int m_nRows = m_nWorldHeight+1;

        m_GridCtrl.SetAutoSizeStyle();

	    TRY {
		    m_GridCtrl.SetRowCount(m_nRows);
		    m_GridCtrl.SetColumnCount(m_nCols);
		    m_GridCtrl.SetFixedRowCount(m_nFixRows);
		    m_GridCtrl.SetFixedColumnCount(m_nFixCols);
	    }
	    CATCH (CMemoryException, e)
	    {
	    	e->ReportError();
    		return false;
	    }
        END_CATCH

	    // fill rows/cols with text
    	for (int col = 0; col < m_GridCtrl.GetColumnCount(); col++)
        {
			m_GridCtrl.SetColumnWidth( col, 30 );
			for (int row = 0; row < m_GridCtrl.GetRowCount(); row++)
		    { 
                CString str;

			    GV_ITEM Item;

    			Item.mask = GVIF_TEXT;
	    		Item.row = row;
		    	Item.col = col;

			    if (row < m_nFixRows)
                    str.Format(_T("%d"),col);
                else if (col < m_nFixCols) 
                    str.Format(_T("%d"), row);
                else
				{
					if( pObjMap[ (m_nWorldWidth)*(row-1) + (col-1) ].type == EOBJTYPE_CREATURE )
						str.Format(_T("%d"), pObjMap[ (m_nWorldWidth)*(row-1) + (col-1) ].nLife);
					else
						str.Format(_T("%d"), pObjMap[ (m_nWorldWidth)*(row-1) + (col-1) ].nAge);

					COLORREF clr;
					switch( pObjMap[ (m_nWorldWidth)*(row-1) + (col-1) ].type )
					{
					case EOBJTYPE_FOOD:
						clr = RGB( 0, 200, 0 );
						break;
					case EOBJTYPE_POISON:
						clr = RGB( 100, 100, 100 );
						break;
					case EOBJTYPE_CREATURE:
						clr = RGB( 200, 0, 0 );
						break;
					default:
						clr = RGB( 255, 255, 255 );
						break;
					}
					Item.crBkClr = clr;
					Item.mask    |= (GVIF_BKCLR|GVIF_FGCLR);
				}
                Item.strText = str;

        		m_GridCtrl.SetItem(&Item);

				m_GridCtrl.SetRowHeight( row, 22 );
	    	}
        }
    }

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDetailDlg::OnDestroy()
{
	CDialog::OnDestroy();

	delete this;
}

BOOL CDetailDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
	if (wParam == (WPARAM)m_GridCtrl.GetDlgCtrlID())
    {
        *pResult = 1;
        NM_GRIDVIEW *pGridView = (NM_GRIDVIEW*)lParam;
        if (GVN_SELCHANGED == pGridView->hdr.code)
        {
            //TRACE2("Getting Display info for cell %d,%d\n", 
			m_nCellAge = pObjMap[ (m_nWorldWidth)*(pGridView->iRow-1) + (pGridView->iColumn-1) ].nAge;
			m_nCellLife = pObjMap[ (m_nWorldWidth)*(pGridView->iRow-1) + (pGridView->iColumn-1) ].nLife;

			UpdateData( FALSE );

            return TRUE;
        }
    }
	return CDialog::OnNotify(wParam, lParam, pResult);
}

void CDetailDlg::OnBnClickedAgesearch()
{
	UpdateData();

	m_listAgeResult.ResetContent();
	for( int i=0; i<m_nWorldWidth*m_nWorldHeight; i++ )
	{
		if( pObjMap[i].nAge == m_nAgeToFind )
		{
			CString res;
			res.Format( "%d,%d", i%m_nWorldWidth+1, i/m_nWorldWidth+1 );
			int idx = m_listAgeResult.AddString( res );
			m_listAgeResult.SetItemData( idx, i );
		}
	}
}

void CDetailDlg::OnLbnDblclkAgeresultlist()
{
	int idx = m_listAgeResult.GetCurSel( );

	m_GridCtrl.SetFocusCell( m_listAgeResult.GetItemData(idx)/m_nWorldWidth+1, m_listAgeResult.GetItemData(idx)%m_nWorldWidth+1 );
}

void CDetailDlg::OnLbnSelchangeAgeresultlist()
{
	int data = m_listAgeResult.GetItemData(m_listAgeResult.GetCurSel( ));

	m_GridCtrl.SetFocusCell( data/m_nWorldWidth+1, data%m_nWorldWidth+1 );

	m_nCellAge = pObjMap[ data ].nAge;
	m_nCellLife = pObjMap[ data ].nLife;
	UpdateData( FALSE );
}

void CDetailDlg::OnBnClickedLifesearch()
{
	UpdateData();

	m_listLifeResult.ResetContent();
	for( int i=0; i<m_nWorldWidth*m_nWorldHeight; i++ )
	{
		if( pObjMap[i].nLife == m_nLifeToFind )
		{
			CString res;
			res.Format( "%d,%d", i%m_nWorldWidth+1, i/m_nWorldWidth+1 );
			int idx = m_listLifeResult.AddString( res );
			m_listLifeResult.SetItemData( idx, i );
		}
	}
}

void CDetailDlg::OnLbnSelchangeLiferesultlist()
{
	int data = m_listLifeResult.GetItemData(m_listLifeResult.GetCurSel( ));

	m_GridCtrl.SetFocusCell( data/m_nWorldWidth+1, data%m_nWorldWidth+1 );

	m_nCellAge = pObjMap[ data ].nAge;
	m_nCellLife = pObjMap[ data ].nLife;
	UpdateData( FALSE );
}
